package com.ch.sys.utils;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.BeanUtils;

public class BeanHelper extends BeanUtils {

    private static final Log logger = LogFactory.getLog(BeanHelper.class);

    public static <T> T copyProperties(Object source, Class<T> target) {
    	T entity = null;
		try {
			entity = target.newInstance();
			BeanUtils.copyProperties(source, entity);
		} catch (Exception e) {
			BeanHelper.logger.error(e);
		} 
    	
    	return entity;
    }
    
    public static <E> List<E> copyTo(List<?> sources, Class<E> target) {
        if (sources.size() == 0) {
            return Collections.emptyList();
        }

        List<E> list = new ArrayList<E>(sources.size());

        try {
            for (Object o : sources) {
                E e = target.newInstance();
                BeanUtils.copyProperties(o, e);
                list.add(e);
            }
        } catch (Exception e) {
            BeanHelper.logger.error(e);
        }

        return list;
    }
}
